Page({
  data: {
    timer: 30, // 默认倒计时时间
    questions: [], // 存储本场比赛的随机问题数组
    currentQuestionIndex: 0, // 当前题目索引
    userScore: 0, // 用户得分
    opponentScore: 0, // 人机得分
    userAnswer: '', // 用户答案
    totalQuestions: 10, // 本场比赛题数
    currentQuestion: '', // 当前问题内容
    questionType: ['+', '-', '*', '/'] // 可用的运算符
  },

  onLoad: function () {
    this.generateQuestions(); // 生成随机题目
    this.startCompetition(); // 直接开始竞赛
  },

  generateQuestions: function () {
    const questions = [];
    for (let i = 0; i < this.data.totalQuestions; i++) {
      const question = this.generateRandomQuestion();
      questions.push(question);
    }
    this.setData({
      questions: questions,
      currentQuestion: questions[0].question, // 初始化当前问题
    });
  },

  generateRandomQuestion: function () {
    const num1 = Math.floor(Math.random() * 10) + 1; // 1到10的随机数
    const num2 = Math.floor(Math.random() * 10) + 1; // 1到10的随机数
    const operator = this.data.questionType[Math.floor(Math.random() * this.data.questionType.length)];

    let question = '';
    let answer;

    switch (operator) {
      case '+':
        question = `${num1} + ${num2} = ?`;
        answer = num1 + num2;
        break;
      case '-':
        question = `${num1} - ${num2} = ?`;
        answer = num1 - num2;
        break;
      case '*':
        question = `${num1} * ${num2} = ?`;
        answer = num1 * num2;
        break;
      case '/':
        // 避免除数为0，且让结果为整数
        const multiple = num1 * num2;
        question = `${multiple} / ${num2} = ?`;
        answer = multiple / num2;
        break;
    }

    return { question: question, answer: answer };
  },

  startCompetition: function () {
    this.setData({
      userScore: 0,
      opponentScore: 0,
      currentQuestionIndex: 0,
      timer: 30,
    });

    this.loadNextQuestion(); // 加载第一题
  },

  loadNextQuestion: function () {
    const index = this.data.currentQuestionIndex;

    if (index < this.data.totalQuestions) {
      const nextQuestion = this.data.questions[index];
      this.setData({
        currentQuestion: nextQuestion.question,
        timer: 30, // 重置计时器
        userAnswer: '', // 清空用户答案
      });
    } else {
      this.endCompetition(); // 结束比赛
    }
  },

  endCompetition: function () {
    // 比赛结束时的逻辑，可以根据分数进行判断
    wx.showModal({
      title: '比赛结束',
      content: `你的得分: ${this.data.userScore}, 人机得分: ${this.data.opponentScore}`,
      showCancel: false,
      success: (res) => {
        if (res.confirm) {
          wx.navigateBack(); // 返回上一页或重新开始比赛
        }
      }
    });
  },

  onInput: function (e) {
    this.setData({
      userAnswer: e.detail.value
    });
  },

  submitAnswer: function () {
    const userAnswer = parseFloat(this.data.userAnswer);
    const correctAnswer = this.data.questions[this.data.currentQuestionIndex].answer; // 当前题的正确答案
    if (userAnswer === correctAnswer) {
      this.setData({
        userScore: this.data.userScore + 1,
      });
    } 

    // 题目检查后，进入下一题
    this.setData({
      currentQuestionIndex: this.data.currentQuestionIndex + 1
    });

    // 立即加载下一题
    this.loadNextQuestion(); 
  },
});
